package com.itxiaowu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itxiaowu.dao.DyUserDao;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.pojo.Permission;
import com.itxiaowu.pojo.Role;
import com.itxiaowu.pojo.User;
import com.itxiaowu.service.DyUserService;
import com.itxiaowu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = DyUserService.class)
@Transactional
public class DyUserServiceImpl implements DyUserService {

    @Autowired
    private DyUserDao dyUserDao;

    //分页查询
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page=dyUserDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }
    //显示所有角色 弹出用户框
    @Override
    public List<Role> findAllRole() {
        List<Role> roleList=dyUserDao.findAllRole();
        return roleList;
    }
    //添加用户,先添加用户表,再添加关联的角色
    @Override
    public void addUser(User user, Integer[] roleIds) throws Exception {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password= bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(password);

        dyUserDao.addUser(user);
        //通过添加用户获得用户id
        setUserRole(user.getId(),roleIds);
    }
    //根据id查询用户数据回显
    @Override
    public User queryUserById(Integer id) {
        User user=dyUserDao.queryUserById(id);
        return user;
    }

    //查询所有权限
    @Override
    public List<Role> findAllPermission() {
        List<Role> roleList=dyUserDao.findAllPermission();
        return roleList;
    }

    //修改用户
    @Override
    public void edit(User user, Integer[] roleIds) {
        //先修改用户信息
        dyUserDao.editUser(user);
        //根据用户id  先删除它对应的角色信息
        dyUserDao.deleteUserRoleId(user.getId());
        //根据用户id  添加它所选的角色信息
        setUserRole(user.getId(),roleIds);
    }

    //根据用户用户id 查询所有的角色ids
    @Override
    public Integer[] findRoleIdsById(Integer id) {
        Integer[] ids=dyUserDao.findRoleIdsById(id);
        return ids;
    }

    //根据id删除用户
    @Override
    public void deleteUserById(Integer id) {
            //先删除关联表中的所有用户数据
        dyUserDao.deleteUserRoleId(id);
        //再删除用户
        dyUserDao.deleteUser(id);
    }


    //根据用户id关联用户
    private void setUserRole(Integer id, Integer[] roleIds) {
            //先判断数字是否为null
        if(roleIds!=null||roleIds.length>0){
            Map map = new HashMap<>();
            for(Integer rids:roleIds){
                map.put("user_id", id);
                map.put("role_id",rids);
                dyUserDao.addUserRole(map);
            }


        }
    }


}
